package com.rmy.queue.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.rmy.CompUtils;
import com.rmy.common.core.text.Convert;
import com.rmy.domain.CompArea;
import com.rmy.queue.common.QueueConst;
import com.rmy.queue.common.webSocket.CompShowWebSocketServer;
import com.rmy.queue.domain.QmsVoicConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.rmy.common.annotation.Log;
import com.rmy.common.core.controller.BaseController;
import com.rmy.common.core.domain.AjaxResult;
import com.rmy.common.enums.BusinessType;
import com.rmy.queue.domain.QmsCompConfig;
import com.rmy.queue.service.IQmsCompConfigService;
import com.rmy.common.utils.poi.ExcelUtil;
import com.rmy.common.core.page.TableDataInfo;
import rmy.VoicUtils;
import rmy.domain.VoicCmd;

/**
 * 综合屏配置Controller
 *
 * @author rmy
 * @date 2022-10-03
 */
@RestController
@RequestMapping("/queue/compconfig")
public class QmsCompConfigController extends BaseController
{
    @Autowired
    private IQmsCompConfigService qmsCompConfigService;

    /**
     * 查询综合屏配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(QmsCompConfig qmsCompConfig)
    {
        startPage();
        List<QmsCompConfig> list = qmsCompConfigService.selectQmsCompConfigList(qmsCompConfig);
        return getDataTable(list);
    }

    /**
     * 导出综合屏配置列表
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:export')")
    @Log(title = "综合屏配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QmsCompConfig qmsCompConfig)
    {
        List<QmsCompConfig> list = qmsCompConfigService.selectQmsCompConfigList(qmsCompConfig);
        ExcelUtil<QmsCompConfig> util = new ExcelUtil<QmsCompConfig>(QmsCompConfig.class);
        util.exportExcel(response, list, "综合屏配置数据");
    }

    /**
     * 获取综合屏配置详细信息
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(qmsCompConfigService.selectQmsCompConfigById(id));
    }

    /**
     * 新增综合屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:add')")
    @Log(title = "综合屏配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QmsCompConfig qmsCompConfig)
    {
        qmsCompConfig.setCreateBy(getUsername());
        return toAjax(qmsCompConfigService.insertQmsCompConfig(qmsCompConfig));
    }

    /**
     * 修改综合屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:edit')")
    @Log(title = "综合屏配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QmsCompConfig qmsCompConfig) throws IOException {
        qmsCompConfig.setUpdateBy(getUsername());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("cmd", QueueConst.WEBSOCKET_CMD.CMD_COMP_PUB_NOTICE);
        CompShowWebSocketServer.sendMessage(qmsCompConfig.getOrgNo(), Convert.toStr(qmsCompConfig.getId(), ""), jsonObject.toString());
        return toAjax(qmsCompConfigService.updateQmsCompConfig(qmsCompConfig));
    }

    /**
     * 删除综合屏配置
     */
    @PreAuthorize("@ss.hasPermi('queue:compconfig:remove')")
    @Log(title = "综合屏配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(qmsCompConfigService.deleteQmsCompConfigByIds(ids));
    }


    /**
     * 发送综合屏
     */
    @PostMapping(value = "/test/{id}")
    public AjaxResult test(@PathVariable("id") Long id, @RequestParam String testContent)
    {
        QmsCompConfig qmsCompConfig = qmsCompConfigService.selectQmsCompConfigById(id);
        List<CompArea> compAreaList = new ArrayList<>();
        CompArea compArea = new CompArea();
        compArea.setId((short) 1);
        compArea.setX((short) 0);
        compArea.setY((short) 0);
        compArea.setH((short) 16);
        compArea.setW((short) 160);
        compArea.setContent("车辆排队叫号信息看板");
        compArea.setMode(1);
        compAreaList.add(compArea);
        try {
            CompUtils.sendComp(qmsCompConfig.getDeviceIp(), qmsCompConfig.getDevicePort().intValue(), "{}", compAreaList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }


    /**
     * 测试发送模板
     */
    @PostMapping(value = "/testTemplate/{id}")
    public AjaxResult testTemplate(@PathVariable("id") Long id)
    {
        QmsCompConfig qmsCompConfig = qmsCompConfigService.selectQmsCompConfigById(id);
        List<CompArea> compAreaList = CompUtils.parseContent(qmsCompConfig.getContentTemplate());
        try {
            CompUtils.sendComp(qmsCompConfig.getDeviceIp(), qmsCompConfig.getDevicePort().intValue(), "{}", compAreaList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success();
    }
}
